#!/usr/bin/env python3
# -*- coding: utf-8 -*-

"""
@AUTHOR: tyc
@VERSION: 1.0.0
@DESCRIPTION: xpath搜素xml文档  pip install lxml
@time: 2021-07-03 10:17:18 
"""
from lxml import etree

xml = '''
<book>
    <id>1</id>
    <book_name>spring实战</book_name>
    <author>
        <id>1001</id>
        <name>Craig-Walls</name>
        <div>
            <name>测试1</name>
        </div>
        <div>
            <name>测试2</name>
        </div>
        <div>
            <name>测试3</name>
        </div>
    </author>
    <price>55</price>
    
    <id>2</id>
    <book_name>算法图解</book_name>
    <author>
        <id>1002</id>
        <name>Aditya-Bhargava</name>
    </author>
    <price>25</price>
</book>
'''
tree = etree.XML(xml)
print(tree.xpath("/book"))
# [@href=']属性值筛选
print(tree.xpath("/book/id"))
print(tree.xpath("/book/id=1"))
print(tree.xpath("/book/author/name/text()"))
print(tree.xpath("/book/author//name/text()"))  # // 代表当前以及子标签下
print(tree.xpath("/book/author/*/name/text()"))  # *只在子标签下找
# 可以打开网页直接复制 f12--->copy--->xpath

